package datastructure.linear.list;

import datastructure.IDiscreteCountable;

public class SingleList<T> implements IDiscreteCountable<T> {

	public SingleList(){
		size = 0;
		head = null;
		end = null;
	}
	public SingleList(T[] arr){
		add(arr);
	}
	public void add(T[] arr){
		for(T e : arr)
			add(e);
	}
	public String toString(){
		StringBuffer sb = new StringBuffer();
		Node current = head;
		while(current!=null){
			sb.append(current.getElement());
			sb.append(" ");
			current = current.next;
		}
		return sb.toString();
	}
	@Override
	public int size() {
		return size;
	}
	public void add(T e){
		Node newNode = new Node(e);
		if(size==0){
			head = newNode;
			end = newNode;
			size++;
			return;
		}
		end.setNext(newNode);
		end = newNode;
		size++;
	}
	class Node{
		public Node(){
			next = null;
		}
		public Node(T e){
			setElement(e);
			next = null;
		}
		public T getElement() {
			return element;
		}
		public void setElement(T element) {
			this.element = element;
		}
		public Node getNext() {
			return next;
		}
		public void setNext(Node next) {
			this.next = next;
		}
		private T element;
		private Node next;
	}
	private int size;
	private Node head;
	private Node end;
}
